package com.ruoyi.ziran.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.ziran.domain.*;
import com.ruoyi.ziran.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ziran.mapper.ZrPaperUserMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户答题的试卷Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class ZrPaperUserServiceImpl implements IZrPaperUserService 
{
    @Autowired
    private ZrPaperUserMapper zrPaperUserMapper;
    @Autowired
    private IZrQuestionBankItemService iZrQuestionBankItemService;
    @Autowired
    private IZrQuestionBankService iZrQuestionBankService;
    @Autowired
    private IZrPaperService iZrPaperService;
    @Autowired
    private IZrPaperUserItemService iZrPaperUserItemService;

    /**
     * 查询用户答题的试卷
     * 
     * @param id 用户答题的试卷主键
     * @return 用户答题的试卷
     */
    @Override
    public ZrPaperUser selectZrPaperUserById(Long id)
    {
        return zrPaperUserMapper.selectZrPaperUserById(id);
    }

    /**
     * 查询用户答题的试卷列表
     * 
     * @param zrPaperUser 用户答题的试卷
     * @return 用户答题的试卷
     */
    @Override
    public List<ZrPaperUser> selectZrPaperUserList(ZrPaperUser zrPaperUser)
    {
        List<ZrPaperUser> list=zrPaperUserMapper.selectZrPaperUserList(zrPaperUser);
        list.forEach(e->{
            e.setZrPaper(iZrPaperService.selectZrPaperById(e.getPaperId()));
        });
        return list;
    }

    /**
     * 新增用户答题的试卷
     * 
     * @param zrPaperUser 用户答题的试卷
     * @return 结果
     */
    @Override
    @Transactional
    public int insertZrPaperUser(ZrPaperUser zrPaperUser)
    {
        zrPaperUser.setCreateTime(DateUtils.getNowDate());
        zrPaperUser.setAnswerUserId(ShiroUtils.getUserId().toString());
        zrPaperUser.setAnswerUserName(ShiroUtils.getSysUser().getUserName());
        zrPaperUserMapper.insertZrPaperUser(zrPaperUser);
        if (zrPaperUser.getList()==null||zrPaperUser.getList().size()==0){
            throw new ServiceException("答案不能为空");
        }
        List<ZrPaperUserItem> itemList=zrPaperUser.getList();
        ZrQuestionBank paperBank=new ZrQuestionBank();
        paperBank.setPaperId(zrPaperUser.getPaperId());
        List<ZrQuestionBank> paperBankList=iZrQuestionBankService.selectZrQuestionBankList(paperBank);
        if (paperBankList.size()!=itemList.size()){
            throw new ServiceException("答案未提交完整");
        }
        //计算分数
        Integer size=itemList.size();
        Integer everyScore=100/size;
        Integer sumScore=0;
        for (ZrPaperUserItem item:itemList){
            item.setPaperUserId(zrPaperUser.getId());
            ZrQuestionBank zrQuestionBank=iZrQuestionBankService.selectZrQuestionBankById(item.getQuestionBankId());
            if (zrQuestionBank==null){
                throw new ServiceException("题不存在");
            }
            item.setAnswer(zrQuestionBank.getAnswer());
            if (item.getAnswer().equals(item.getMyAnswer())){
                item.setIsSure(1);
                item.setEveryScore(new Long(everyScore));
                sumScore=sumScore+everyScore;
            }else{
                item.setEveryScore(0L);
                item.setIsSure(0);
            }
            iZrPaperUserItemService.insertZrPaperUserItem(item);
        }
        zrPaperUser.setScore(new BigDecimal(sumScore));
        this.updateZrPaperUser(zrPaperUser);
        return 1;
    }

    /**
     * 修改用户答题的试卷
     * 
     * @param zrPaperUser 用户答题的试卷
     * @return 结果
     */
    @Override
    public int updateZrPaperUser(ZrPaperUser zrPaperUser)
    {
        zrPaperUser.setUpdateTime(DateUtils.getNowDate());
        return zrPaperUserMapper.updateZrPaperUser(zrPaperUser);
    }

    /**
     * 批量删除用户答题的试卷
     * 
     * @param ids 需要删除的用户答题的试卷主键
     * @return 结果
     */
    @Override
    public int deleteZrPaperUserByIds(String ids)
    {
        return zrPaperUserMapper.deleteZrPaperUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户答题的试卷信息
     * 
     * @param id 用户答题的试卷主键
     * @return 结果
     */
    @Override
    public int deleteZrPaperUserById(Long id)
    {
        return zrPaperUserMapper.deleteZrPaperUserById(id);
    }

    @Override
    public List<ZrPaperUserExcel> getExcelList(ZrPaperUser zrPaperUser) {
        return zrPaperUserMapper.getExcelList(zrPaperUser);
    }
}
